home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Snippets / Demo / IconButtons.p < prev    next >
Encoding:
Text File  |  1997-07-22  |  3.2 KB  |  148 lines  |  [TEXT/CWIE]

  1. UNIT IconButtons;
  2. INTERFACE
  3.     
  4.     USES
  5.         Types, Events, Icons, QuickDraw, Memory, IconCache;
  6.         
  7.     TYPE
  8.         IconButton = RECORD
  9.             position: Point;
  10.             bodyIcon: INTEGER;
  11.             hilitedBodyIcon: INTEGER;
  12.             faceIcon: INTEGER;
  13.         END;
  14.         IconButtonPtr = ^IconButton;
  15.         IconButtonHandle = ^IconButtonPtr;
  16.         
  17.         IdleProc = PROCEDURE;
  18.                 
  19.     FUNCTION NewIconButton(h, v, bodyIcon, pressedBodyIcon, faceIcon: INTEGER): IconButtonHandle;
  20.     PROCEDURE DisposeIconButton(button: IconButtonHandle);
  21.     
  22.     FUNCTION HitIconButton(button: IconButtonHandle; pt: Point): BOOLEAN;
  23.     FUNCTION TrackIconButton(button: IconButtonHandle; idle: IdleProc): BOOLEAN;
  24.     
  25.     PROCEDURE DrawIconButton(button: IconButtonHandle);
  26.  
  27. IMPLEMENTATION
  28.  
  29.     USES
  30.         Events;
  31.         
  32.     PROCEDURE DrawIconButton(button: IconButtonHandle);
  33.     VAR
  34.         icon: CIconHandle;
  35.         box: Rect;
  36.         
  37.     BEGIN
  38.         icon := GetCachedIcon(button^^.bodyIcon);
  39.         IF icon <> NIL THEN
  40.             BEGIN
  41.                 box := icon^^.iconPMap.bounds;
  42.                 OffsetRect(box, button^^.position.h, button^^.position.v);
  43.                 
  44.                 PlotCachedIcon(button^^.bodyIcon, box);
  45.                 PlotCachedIcon(button^^.faceIcon, box);
  46.             END;
  47.     END;
  48.     
  49.     FUNCTION TrackIconButton(btn: IconButtonHandle; idle: IdleProc): BOOLEAN;
  50.     VAR
  51.         pt: Point;
  52.         pressed: BOOLEAN;
  53.         box: Rect;
  54.         face, down, body: CIconHandle;
  55.         
  56.     BEGIN
  57.         face := GetCachedIcon(btn^^.faceIcon);
  58.         down := GetCachedIcon(btn^^.hilitedBodyIcon);
  59.         body := GetCachedIcon(btn^^.bodyIcon);
  60.         
  61.         IF (face = NIL) OR (down = NIL) OR (body = NIL) THEN
  62.             BEGIN
  63.                 DebugStr('TrackIconButton: GetCachedIcon');
  64.                 TrackIconButton := FALSE;
  65.                 Exit(TrackIconButton);
  66.             END;
  67.             
  68.         box := body^^.iconPMap.bounds;
  69.         OffsetRect(box, btn^^.position.h, btn^^.position.v);
  70.             
  71.         pressed := FALSE;
  72.         WHILE Button DO
  73.             BEGIN
  74.                 GetMouse(pt);
  75.                 IF idle <> NIL THEN
  76.                     idle;
  77.                 
  78.                 IF PtInRect(pt, box) AND (NOT pressed) THEN
  79.                     BEGIN
  80.                         pressed := TRUE;
  81.                         PlotCIcon(box, down);
  82.                         OffsetRect(box, 1, 1);
  83.                         PlotCIcon(box, face);
  84.                         OffsetRect(box, -1, -1);
  85.                     END;
  86.                     
  87.                 IF NOT PtInRect(pt, box) AND pressed THEN
  88.                     BEGIN
  89.                         pressed := FALSE;
  90.                         PlotCIcon(box, body);
  91.                         PlotCIcon(box, face);
  92.                     END;
  93.             END;
  94.             
  95.         IF pressed THEN
  96.             BEGIN
  97.                 PlotCIcon(box, body);
  98.                 PlotCIcon(box, face);
  99.             END;
  100.             
  101.         TrackIconButton := pressed;
  102.     END;
  103.  
  104.     FUNCTION HitIconButton(button: IconButtonHandle; pt: Point): BOOLEAN;
  105.     VAR
  106.         body: CIconHandle;
  107.         box: Rect;
  108.         
  109.     BEGIN
  110.         body := GetCachedIcon(button^^.bodyIcon);
  111.         IF body = NIL THEN
  112.             BEGIN
  113.                 DebugStr('HitIconButton: GetCachedIcon');
  114.                 HitIconButton := FALSE;
  115.                 Exit(HitIconButton);
  116.             END;
  117.             
  118.         box := body^^.iconPMap.bounds;
  119.         OffsetRect(box, button^^.position.h, button^^.position.v);
  120.         
  121.         HitIconButton := PtInRect(pt, box);
  122.     END;
  123.     
  124.     FUNCTION NewIconButton(h, v, bodyIcon, pressedBodyIcon, faceIcon: INTEGER): IconButtonHandle;
  125.     VAR
  126.         button: IconButtonHandle;
  127.         
  128.     BEGIN
  129.         Handle(button) := NewHandleClear(SizeOf(IconButton));
  130.         IF button <> NIL THEN
  131.             BEGIN
  132.                 button^^.position.h := h;
  133.                 button^^.position.v := v;
  134.                 button^^.bodyIcon := bodyIcon;
  135.                 button^^.hilitedBodyIcon := pressedBodyIcon;
  136.                 button^^.faceIcon := faceIcon;
  137.             END;
  138.             
  139.         NewIconButton := button;
  140.     END;
  141.     
  142.     PROCEDURE DisposeIconButton(button: IconButtonHandle);
  143.     BEGIN
  144.         IF button <> NIL THEN
  145.             DisposeHandle(Handle(button));
  146.     END;
  147.             
  148. END.